package bb.lanxing.lib.devices.ble.device;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallbackCompat;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.provider.DataNotifier;
import java.util.UUID;
import android.util.Log;
public class StandardBiketrainerDevice extends AbsBleDevice {
    final static private String TAG = "StandardBiketrainerDevice";
    final static private boolean DebugPower = false;
    static private int intValuePower = 0;

    public StandardBiketrainerDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new BiketrainerBluetoothGattCallback(this));
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onServicesDiscovered() {
        super.onServicesDiscovered();

        BluetoothGattService service_power = this.mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_SERVICE));
        if (service_power == null) {
        }else{
            Log.d(TAG,"onServicesDiscovered characteristic_power !!! " );
            BluetoothGattCharacteristic characteristic_power = mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_SERVICE)).getCharacteristic(UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_CHAR_MEASURE));
            mBluetoothGatt.readCharacteristic(characteristic_power);
            setCharacteristicNotification(characteristic_power, true);
        }

        BluetoothGattService service_cadence = this.mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_CADENCE_SERVICE));
        if (service_cadence == null) {
        }else{
            Log.d(TAG,"onServicesDiscovered service_cadence !!! " );
            BluetoothGattCharacteristic characteristic_cadence = mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_CADENCE_SERVICE)).getCharacteristic(UUID.fromString(BLEAttributes.BLE_CADENCE_MEASUREMENT));
            mBluetoothGatt.readCharacteristic(characteristic_cadence);
            setCharacteristicNotification(characteristic_cadence, true);
        }
    }

    private static class BiketrainerBluetoothGattCallback extends SimpleBluetoothGattCallbackCompat<StandardBiketrainerDevice> {
        BiketrainerBluetoothGattCallback(StandardBiketrainerDevice standardBiketrainerDevice) {
            super(standardBiketrainerDevice);
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            int intValueCumulativeCrankRevolution = 0;
            int intValueLastCrankEventTime = 0;

            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);

            Log.d(TAG,"onCharacteristicChanged " );
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            int type = ((StandardBiketrainerDevice) this.mClientRef).getType();
            if (UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_CHAR_MEASURE).equals(uuid)) {
                if(DebugPower) {
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);;
                }
                else{
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 2);
                }
            }

            if (UUID.fromString(BLEAttributes.BLE_CADENCE_MEASUREMENT).equals(uuid)) {
                intValueCumulativeCrankRevolution = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 1);
                intValueLastCrankEventTime = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3);
                Log.d(TAG,"intValuePower is "+ intValuePower);
                Log.d(TAG,"intValueCumulativeCrankRevolution is "+ intValueCumulativeCrankRevolution);
                Log.d(TAG,"intValueLastCrankEventTime is "+ intValueLastCrankEventTime);
                DataNotifier.notifyBiketrainerData(type, ((StandardBiketrainerDevice) this.mClientRef).getAddress(),intValuePower, intValueCumulativeCrankRevolution,intValueLastCrankEventTime);
            }
        }
    }
}
